/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.tigris.jasmincc.implementation;

import java.util.*;

import org.tigris.jasmincc.*;
import org.tigris.jasmincc.framework.*;
import org.tigris.mappedlexer.validator.VerifyHub;

/**
 * 
 * @author Sam Huang
 * 
 */
public class SimpleRegularExpressionProduction extends AbstractProduction
		implements StorableProduction {

	protected Map<String, Object> cache = null;

	protected NamingManager namingManager = null;

	protected List<String> tokens = null;

	protected int type;

	private String key;

	public void parse() throws Exception {
		namingManager = (NamingManager) cache.get("namingManager");

		tokens = new ArrayList<String>();

		tk = lexer.nexttok(false);
		if (tk.equals("\"")) {
			tokens.add(lexer.skipQutedString());
		} else if (tk.equals("<")) {
			tk = lexer.nexttok(false);
			do {
				if (tk.equals("#")) {
					/*
					 * ( "<" [ [ "#" ] identifier() ":" ]
					 * complex_regular_expression_choices() ">" )
					 */
					getSubToken();
				} else if (tk.equals("EOF")) {
					lexer.print("EOF");
				} else if (VerifyHub.IDENTIFIER.validate(tk)) {
					getToken();
				} else if (tk.equals("\"")) {
					// tokens[0] = lexer.skipQutedString();
				}
				tk = lexer.nexttok(false);
			} while (!tk.equals(">"));
		}
	}

	private void getToken() throws Exception {
		this.key = tk;
		key = namingManager.updateRef(key);

		String nexttk = lexer.nexttok(false);
		if (nexttk.equals(":")) {
			tk = lexer.skipTo('<');

			tk = org.tigris.jasmincc.utils.StringServices.

			decorate(tk);
			tk = change(tk);

			// tokens[0] = key;
			// tokens[1] = tk;
		} else {
			// ( "<" identifier() ">" )
			lexer.nexttok(false, "<");
			tk = lexer.nexttok(false, VerifyHub.IDENTIFIER);
			// tokens[0] = key;
			// tokens[1] = tk;

			lexer.nexttok(false, ">");
		}
	}

	private void getSubToken() throws Exception {
		this.key = lexer.nexttok(false, VerifyHub.IDENTIFIER);
		key = "#" + namingManager.updateRef(key);

		lexer.nexttok(false, ":");
		tk = lexer.skipTo('<');
		// tk = change(tk);
		tk = org.tigris.jasmincc.utils.StringServices.decorate(tk);

		// tokens[0] = key;
		// tokens[1] = tk;
	}

	/**
	 * @return the list
	 */
	public List<String> getTokens() {
		return this.tokens;
	}

	/**
	 * @param cache
	 *            the cache to set
	 */
	public void setCache(Map<String, Object> cache) {
		this.cache = cache;
	}

	protected String change(String tk) {
		StringBuffer mirror = new StringBuffer();

		for (char chr : tk.toCharArray()) {
			mirror.append(chr);
			if (chr == '<') {
				if (namingManager.getInstanceName().length() > 0) {
					mirror.append(namingManager.getInstanceName()).append("_");
				}
			}
		}
		return mirror.toString();
	}

}
